home *** CD-ROM | disk | FTP | other *** search
/ IRIX Base Documentation 1998 November / IRIX 6.5.2 Base Documentation November 1998.img / usr / share / catman / p_man / cat3 / perl5 / IO::Select.z / IO::Select
Text File  |  1998-10-30  |  6KB  |  199 lines

  1.  
  2.  
  3.  
  4. IIIIOOOO::::::::SSSSeeeelllleeeecccctttt((((3333))))                                                    IIIIOOOO::::::::SSSSeeeelllleeeecccctttt((((3333))))
  5.  
  6.  
  7.  
  8. NNNNAAAAMMMMEEEE
  9.      IO::Select - OO interface to the select system call
  10.  
  11. SSSSYYYYNNNNOOOOPPPPSSSSIIIISSSS
  12.          use IO::Select;
  13.  
  14.          $s = IO::Select->new();
  15.  
  16.          $s->add(\*STDIN);
  17.          $s->add($some_handle);
  18.  
  19.          @ready = $s->can_read($timeout);
  20.  
  21.          @ready = IO::Select->new(@handles)->read(0);
  22.  
  23.  
  24. DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
  25.      The IO::Select package implements an object approach to the system select
  26.      function call. It allows the user to see what IO handles, see the
  27.      _I_O::_H_a_n_d_l_e manpage, are ready for reading, writing or have an error
  28.      condition pending.
  29.  
  30. CCCCOOOONNNNSSSSTTTTRRRRUUUUCCCCTTTTOOOORRRR
  31.      new ( [ HANDLES ] )
  32.          The constructor creates a new object and optionally initialises it
  33.          with a set of handles.
  34.  
  35. MMMMEEEETTTTHHHHOOOODDDDSSSS
  36.      add ( HANDLES )
  37.          Add the list of handles to the IO::Select object. It is these values
  38.          that will be returned when an event occurs. IO::Select keeps these
  39.          values in a cache which is indexed by the fileno of the handle, so if
  40.          more than one handle with the same fileno is specified then only the
  41.          last one is cached.
  42.  
  43.          Each handle can be an IO::Handle object, an integer or an array
  44.          reference where the first element is a IO::Handle or an integer.
  45.  
  46.      remove ( HANDLES )
  47.          Remove all the given handles from the object. This method also works
  48.          by the fileno of the handles. So the exact handles that were added
  49.          need not be passed, just handles that have an equivalent fileno
  50.  
  51.      exists ( HANDLE )
  52.          Returns a true value (actually the handle itself) if it is present.
  53.          Returns undef otherwise.
  54.  
  55.      handles
  56.          Return an array of all registered handles.
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.                                                                         PPPPaaaaggggeeee 1111
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. IIIIOOOO::::::::SSSSeeeelllleeeecccctttt((((3333))))                                                    IIIIOOOO::::::::SSSSeeeelllleeeecccctttt((((3333))))
  71.  
  72.  
  73.  
  74.      can_read ( [ TIMEOUT ] )
  75.          Return an array of handles that are ready for reading. TIMEOUT is the
  76.          maximum amount of time to wait before returning an empty list. If
  77.          TIMEOUT is not given and any handles are registered then the call
  78.          will block.
  79.  
  80.      can_write ( [ TIMEOUT ] )
  81.          Same as can_read except check for handles that can be written to.
  82.  
  83.      has_error ( [ TIMEOUT ] )
  84.          Same as can_read except check for handles that have an error
  85.          condition, for example EOF.
  86.  
  87.      count ()
  88.          Returns the number of handles that the object will check for when one
  89.          of the can_ methods is called or the object is passed to the select
  90.          static method.
  91.  
  92.      bits()
  93.          Return the bit string suitable as argument to the core _s_e_l_e_c_t() call.
  94.  
  95.      bits()
  96.          Return the bit string suitable as argument to the core _s_e_l_e_c_t() call.
  97.  
  98.      select ( READ, WRITE, ERROR [, TIMEOUT ] )
  99.          select is a static method, that is you call it with the package name
  100.          like new. READ, WRITE and ERROR are either undef or IO::Select
  101.          objects. TIMEOUT is optional and has the same effect as for the core
  102.          select call.
  103.  
  104.          The result will be an array of 3 elements, each a reference to an
  105.          array which will hold the handles that are ready for reading, writing
  106.          and have error conditions respectively. Upon error an empty array is
  107.          returned.
  108.  
  109. EEEEXXXXAAAAMMMMPPPPLLLLEEEE
  110.      Here is a short example which shows how IO::Select could be used to write
  111.      a server which communicates with several sockets while also listening for
  112.      more connections on a listen socket
  113.  
  114.          use IO::Select;
  115.          use IO::Socket;
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.                                                                         PPPPaaaaggggeeee 2222
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. IIIIOOOO::::::::SSSSeeeelllleeeecccctttt((((3333))))                                                    IIIIOOOO::::::::SSSSeeeelllleeeecccctttt((((3333))))
  137.  
  138.  
  139.  
  140.          $lsn = new IO::Socket::INET(Listen => 1, LocalPort => 8080);
  141.          $sel = new IO::Select( $lsn );
  142.  
  143.          while(@ready = $sel->can_read) {
  144.              foreach $fh (@ready) {
  145.                  if($fh == $lsn) {
  146.                      # Create a new socket
  147.                      $new = $lsn->accept;
  148.                      $sel->add($new);
  149.                  }
  150.                  else {
  151.                      # Process socket
  152.  
  153.                      # Maybe we have finished with the socket
  154.                      $sel->remove($fh);
  155.                      $fh->close;
  156.                  }
  157.              }
  158.          }
  159.  
  160.  
  161. AAAAUUUUTTTTHHHHOOOORRRR
  162.      Graham Barr <_G_r_a_h_a_m._B_a_r_r@_t_i_u_k._t_i._c_o_m>
  163.  
  164. CCCCOOOOPPPPYYYYRRRRIIIIGGGGHHHHTTTT
  165.      Copyright (c) 1995 Graham Barr. All rights reserved. This program is free
  166.      software; you can redistribute it and/or modify it under the same terms
  167.      as Perl itself.
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.                                                                         PPPPaaaaggggeeee 3333
  196.  
  197.  
  198.  
  199.